package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaGuessConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_RECEIVE_GUESS_REWARD_FROM_CROSS, desc = "海上争霸 南海丝路 领取竞猜奖励 跨服返回区服 协议")
public class SeacraftSouthSeaReceiveGuessRewardFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaReceiveGuessRewardCrossToGameMsg crossToGameMsg = SeacraftSouthSeaProto.SeacraftSouthSeaReceiveGuessRewardCrossToGameMsg.parseFrom(packet.getBytes());
        SeacraftSouthSeaProto.SeacraftSouthSeaReceiveGuessRewardRespMsg.Builder stcMsg = SeacraftSouthSeaProto.SeacraftSouthSeaReceiveGuessRewardRespMsg.newBuilder();
        stcMsg.setRet(crossToGameMsg.getRet());
        if(crossToGameMsg.getRet() == 0){
            // 根据 conditionType 取conditionInfoList
            List<SeacraftSouthSeaGuessConfig> list = SeacraftSouthSeaMgr.getConfig().getGuessRoundListMap().get(crossToGameMsg.getRound());
            if(list != null){
                SeacraftSouthSeaGuessConfig reward = null;
                for (SeacraftSouthSeaGuessConfig item : list){
                    // 根据个数取到对应conditionInfo
                    if (item.getCount() <= crossToGameMsg.getNum()) {
                        reward = item;
                    }else {
                        break;
                    }
                }
                if(reward != null){
                    // 加奖励
                    player.getModule(CurrencyModule.class).addCurrency(reward.getAward(), eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityGuessReward);
                    stcMsg.setReward(reward.getAward());
                }
            }
        }
        player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_RECEIVE_GUESS_REWARD, stcMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
